<?php
/**
 * 列表多项选择
 */

namespace generate\field;

class MultiSelect extends Field
{
    public static $html = <<<EOF
<div class="layui-form-item">
<label for="nickname" class="layui-form-label">[FORM_NAME]</label>
<div class="layui-input-block">
    <div id="[FIELD_NAME]"></div>
</div>
</div>
<script>
var [FIELD_NAME] = xmSelect.render({
    el: '#[FIELD_NAME]', 
    filterable: true,
    name:'[FIELD_NAME]',
    toolbar: {show: true},
    autoRow: true,
    data: [
    {foreach name='[FIELD_NAME]' item='item'}
        {name: '{\$item.name}', value: {\$item.id},{if isset(\$data) && in_array(\$item.id,\$data.[FIELD_NAME])}selected:true{/if}},
        {/foreach}
    ]
})
</script>
\n
EOF;
public static $modelAttrCode = <<<EOF
protected function get[FIELD_NAME]Attr(\$value)
{
    return explode(',', \$value);
}
\n
EOF;
    public static $rules = [
        'required' => '非空',
        'regular'  => '自定义正则'
    ];


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace(array('[FORM_NAME]', '[FIELD_NAME]'), array($data['form_name'], $data['field_name']), $html);
        return $html;
    }
}